package com.example.userregist;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private EditText et_name;
    private EditText et_password;
    private Button button;
    private Button but_cancel;
    private UserDatabaseHelper userDatabaseHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //1.获取相应控件
        et_name = findViewById(R.id.et_name);
        et_password = findViewById(R.id.et_password);
        button = findViewById(R.id.button);

        userDatabaseHelper = new UserDatabaseHelper(this);
        readUser();
        //2.给按钮设置点击事件
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                passDate();
            }
        });
        but_cancel = findViewById(R.id.but_cancel);
        but_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //1.创建意图对象
                Intent intent = new Intent(MainActivity.this,RegisteredActivity.class);
                //2.开启意图
                startActivity(intent);
            }
        });
    }
    //保存用户信息
    private void saveUser(){
        SharedPreferences sp = getSharedPreferences("user",MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();//获取编辑器
        //存入数据
        editor.putString("name",et_name.getText().toString());
        editor.putString("password",et_password.getText().toString());
        editor.apply();//提交修改
        Toast.makeText(this,"用户信息保存成功！",Toast.LENGTH_SHORT).show();//弹出提示信息
    }
    //读取用户信息
    private void readUser(){
        SharedPreferences sp = getSharedPreferences("user",MODE_PRIVATE);
        String name = sp.getString("name","请输入用户名！");
        String password = sp.getString("password","请输入用户密码！");
        et_name.setText(name);
        et_password.setText(password);
    }

    //通过这个方法实现数据的传递
    private void passDate() {

        if (userDatabaseHelper.queryforLogin(et_name.getText().toString(), et_password.getText().toString())) {
            //1.创建意图对象
            Intent intent = new Intent(this, ShowActivity.class);
            //2.把数据存入intent
            intent.putExtra("name", et_name.getText().toString().trim());
            intent.putExtra("password", et_password.getText().toString().trim());
            saveUser();
            //3.开启意图
            //startActivity(intent);
            startActivityForResult(intent, 1);
        }else{
            Toast.makeText(MainActivity.this,"用户不存在或密码错误！",Toast.LENGTH_SHORT).show();
        }
    }
    //实现用户数据回传
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        User user = (User) data.getSerializableExtra("User");
        et_name.setText(user.getName());
        et_password.setText(user.getPassword());
    }
}
